<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/11/14
 * Time: 12:24
 */

class AhpCityController extends BaseController{

    /**
     * @Author:      sqq
     * @DateTime:    2018-11-14 12:24:28
     * @Description: 城市列表
     * @param  $token   用户token
     * @link: http://www.activity.com/ahp/ahp3.0/city/cityList?token=5825b9b3e900d9ee45c305fc7b2f4687&page=1&page_size=10&cms_city_name=%E5%8C%97&cms_status=1&status=1
     */
    public function cityList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>'required',
                'page' =>'integer',
                'page_size' =>'integer',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ahp_users=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ahp_users)){
                break;
            }

            //获取参数
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            //查询cms的城市列表
            $cms_city = ResourceCityType::select('id','name','status','short_name','lat','lon')->get();
            if(!is_null($cms_city)){
                foreach($cms_city as $key => $val){
                    $city = AhpCity::where('cms_city_id',$val->id)->first();
                    try{
                        if(is_null($city)){
                            $city = New AhpCity();
                            $city->cms_city_id = $val->id;
                            $city->status = 1;
                            $city->created_at = Common::currentTimeString();
                            $city->created_by = $ahp_users->id;
                            $city->lat = $val->lat;
                            $city->lon = $val->lon;
                        }
                        $city->cms_city_name = $val->name;
                        $city->short_name = $val->short_name;
                        $city->cms_status = $val->status;
                        $city->updated_at = Common::currentTimeString();
                        $city->updated_by = $ahp_users->id;
                        $city->save();
                    }catch (Exception $e){
                        Log::error("error", array("system" => "ahp3.0" ,"event" => "/city/cityList", 'input' =>Input::all() ,"exception" => $e));
                        $response = Lang::get("ahp_error.wechat3_synchronization_city_error_60000");
                        break;
                    }
                }
            }

            $citys = AhpCity::leftjoin('ahp_user','ahp_city.updated_by','=','ahp_user.id')->select("ahp_city.*","ahp_user.real_name")->orderBy('ahp_city.id','asc');
            if(Input::get('cms_city_name')){
                $citys->where("cms_city_name","LIKE","%".Input::get('cms_city_name')."%");
            }
            if(Input::get('cms_status') != ''){
                $citys->where("cms_status",Input::get('cms_status'));
            }
            if(Input::get('status')){
                $citys->where("ahp_city.status",Input::get('status'));
            }
            if(Input::get('cms_city_id')){
                $citys->where("ahp_city.cms_city_id",Input::get('cms_city_id'));
            }

            $count = $citys->count();
            $citys=$citys->skip($page_size*($page-1))->take($page_size)->get();

            //组织返回
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"cityList"=>$citys));
        }while(false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "/city/cityList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      sqq
     * @DateTime:    2018-11-14 15:41:39
     * @Description: cms城市列表（下拉菜单）
     * @link: http://www.activity.com/ahp/ahp3.0/city/cityAll?token=5825b9b3e900d9ee45c305fc7b2f4687
     */
    public function cityAll(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ahp_users=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ahp_users)){
                break;
            }

            $city = ResourceCityType::select('id','name')->get();

            //组织返回
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $city);
        } while (false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "/city/store_city", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-11-14 17:36:39
     * @Description: cms城市限行
     *  {
     *       "0": {
     *           "id": '1',
     *           "week": "周一",
     *           "limit_car": "1和9"
     *       },
     *       "1": {
     *           "id": '2',
     *           "week": "周二",
     *           "limit_car": "2和8"
     *       },
     *       "3": {
     *           "id": '3',
     *           "week": "周三",
     *           "limit_car": "3和7"
     *       },
     *       "4": {
     *           "id": '4',
     *           "week": "周四",
     *           "limit_car": "4和6"
     *       },
     *       "5": {
     *           "id": '5',
     *           "week": "周五",
     *           "limit_car": "5和0"
     *       },
     *       "6": {
     *           "id": '6',
     *           "week": "周六",
     *           "limit_car": "不限行"
     *       },
     *       "7": {
     *           "id": '7',
     *           "week": "周日",
     *           "limit_car": "不限行"
     *       }
     *   }
     * @link: http://www.activity.com/ahp/ahp3.0/city/city_limit?token=5825b9b3e900d9ee45c305fc7b2f4687&ahp_city_id=1&cms_city_id=1&data={%20%220%22:%20{%20%22id%22:%201,%20%22week%22:%20%22%E5%91%A8%E4%B8%80%22,%20%22limit_car%22:%20%221%E5%92%8C9%22%20},%20%221%22:%20{%20%22id%22:%202,%20%22week%22:%20%22%E5%91%A8%E4%BA%8C%22,%20%22limit_car%22:%20%222%E5%92%8C8%22%20},%20%223%22:%20{%20%22id%22:%203,%20%22week%22:%20%22%E5%91%A8%E4%B8%89%22,%20%22limit_car%22:%20%223%E5%92%8C7%22%20},%20%224%22:%20{%20%22id%22:%204,%20%22week%22:%20%22%E5%91%A8%E5%9B%9B%22,%20%22limit_car%22:%20%224%E5%92%8C6%22%20},%20%225%22:%20{%20%22id%22:%205,%20%22week%22:%20%22%E5%91%A8%E4%BA%94%22,%20%22limit_car%22:%20%225%E5%92%8C0%22%20},%20%226%22:%20{%20%22id%22:%206,%20%22week%22:%20%22%E5%91%A8%E5%85%AD%22,%20%22limit_car%22:%20%22%E4%B8%8D%E9%99%90%E8%A1%8C%22%20},%20%227%22:%20{%20%22id%22:%207,%20%22week%22:%20%22%E5%91%A8%E6%97%A5%22,%20%22limit_car%22:%20%22%E4%B8%8D%E9%99%90%E8%A1%8C%22%20}%20}
     */
    public function city_limit(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'ahp_city_id' => 'required',
                'cms_city_id' => 'required',
                'data' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do {
            if ($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ahp_users=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ahp_users)){
                break;
            }

            //获取参数
            $ahp_city_id = Input::get('ahp_city_id');
            $cms_city_id = Input::get('cms_city_id');
            $data = Input::get('data');

//            $arr = array(
//                '0' =>[
//                    'id' => '1',
//                    'week' => '周一',
//                    'limit_car' => '1和9'
//                ],
//                '1' =>[
//                    'id' => '2',
//                    'week' => '周二',
//                    'limit_car' => '2和8'
//                ],
//                '3' =>[
//                    'id' => '3',
//                    'week' => '周三',
//                    'limit_car' => '3和7'
//                ],
//                '4' =>[
//                    'id' => '4',
//                    'week' => '周四',
//                    'limit_car' => '4和6'
//                ],
//                '5' =>[
//                    'id' => '5',
//                    'week' => '周五',
//                    'limit_car' => '5和0'
//                ],
//                '6' =>[
//                    'id' => '6',
//                    'week' => '周六',
//                    'limit_car' => '不限行'
//                ],
//                '7' =>[
//                    'id' => '7',
//                    'week' => '周日',
//                    'limit_car' => '不限行'
//                ],
//            );
//            return json_encode($arr);
            $data = json_decode($data,true);
            if(!empty($data)){
                if(count($data) == count($data, 1)){
                    $response = Lang::get("ahp_error.ahp_city_limit_car_not_array_60001");
                    break;
                }
                foreach($data as $key => $val){
                    $city = AhpCityLimitCar::where('cms_city_id',$cms_city_id)->where('week_id',$val['id'])->first();
                    if(is_null($city)){
                        $city = new AhpCityLimitCar;
                        $city->cms_city_id = $cms_city_id;
                        $city->ahp_city_id = $ahp_city_id;
                        $city->created_at = Common::currentTimeString();
                        $city->created_by = $ahp_users->id;
                    }
                    $city->week_id = isset($val['id']) ? $val['id'] : '';
                    $city->week = isset($val['week']) ? $val['week'] : '';
                    $city->limit_car = isset($val['limit_car']) ? $val['limit_car'] : '';
                    $city->limit_desc = isset($val['limit_desc']) ? $val['limit_desc'] : '';
                    $city->updated_at = Common::currentTimeString();
                    $city->updated_by = $ahp_users->id;

                    try{
                        $city->save();
                    }catch (Exception $e){
                        Log::error("error", array("system" => "ahp3.0" ,"event" => "/store/city_limit", 'input' =>Input::all() ,"exception" => $e));
                        $response = Lang::get("ahp_error.ahp_city_limit_car_save_error_60002");
                        break;
                    }
                }
            }

            //组织返回
            $response = array('code' => 0, 'msg' => 'ok');
        } while (false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "/city/city_limit", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-11-15 12:02:39
     * @Description: cms城市限行配置详情
     * @link: http://www.activity.com/ahp/ahp3.0/city/cityLimitShow?token=5825b9b3e900d9ee45c305fc7b2f4687&cms_city_id=2
     */
    public function cityLimitShow(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'cms_city_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $ahp_users=$this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($ahp_users)){
                break;
            }

            //获取参数
            $cms_city_id = Input::get('cms_city_id');

            $cityLimit = AhpCityLimitCar::where('cms_city_id',$cms_city_id)->where('status',1)->get();

            //组织返回
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $cityLimit);
        } while (false);
        Log::info("info", array("system" => "ahp3.0" ,"event" => "/city/cityLimitShow", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}